/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-12-14     laowang       the first version
 */
#include "usb.h"
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"

#ifndef USB_CONNECT_PIN_NUM
    #define USB_CONNECT_PIN_NUM         GET_PIN(A, 15)  /* PA15 */
#endif

void usb_connect(void)
{
    rt_pin_write(USB_CONNECT_PIN_NUM, PIN_LOW);
}

void usb_disconnect(void)
{
    rt_pin_write(USB_CONNECT_PIN_NUM, PIN_HIGH);
}

static int usb_init(void)
{
    //USBD 接口与 CAN 接口的同时使用
    *((uint32_t*)(USB_BASE + 0x1000)) = 0x00000001;
    /* USB连接引脚为输出模式 */
    rt_pin_mode(USB_CONNECT_PIN_NUM, PIN_MODE_OUTPUT);
    /* 设置低电平 */
    rt_pin_write(USB_CONNECT_PIN_NUM, PIN_HIGH);
#ifdef RT_USB_DEVICE_CDC
    usb_connect();
#endif
    return RT_EOK;
}
INIT_COMPONENT_EXPORT(usb_init);
